﻿/// <reference path="~/ExtClient/ext/ext-all-debug-w-comments.js" />

Ext.Loader.setConfig({
    enabled: true,
    'app': '/ExtClient/app',
    disableCaching:true                //为true时在Get请求时会增加_dt参数，使得Controller的Action必需要有_dt才能匹配
});

Ext.Ajax.disableCaching = false;

Ext.form.Field.prototype.msgTarget = 'side';            // turn on validation errors beside the field globally

//---------begin override form<-->model validation-------------------
//来源：http://existdissolve.com/2012/04/extjs-4-applying-model-validations-to-forms/

Ext.data.validations.minMessage = 'must be greater than or equal to the specified value';
Ext.data.validations.min = function (config, value) {
    if (!Ext.isNumeric(value) || value < config.min) {
        return false;
    }
    return true;
}
Ext.data.validations.maxMessage = 'must be less than or equal to the specified value';
Ext.data.validations.max = function (config, value) {
    if (!Ext.isNumeric(value) || value > config.max) {
        return false;
    }
    return true;
}
Ext.data.validations.rangeMessage = 'must be between the specified values';
Ext.data.validations.range = function (config, value) {
    if (!Ext.isNumeric(value) || (value > config.max || value < config.min)) {
        return false;
    }
    return true;
}

Ext.override(Ext.form.field.Base, {
    setModelFieldValidation: function (validation) {
        this.modelValidations = Ext.isArray(this.modelValidations) ? this.modelValidations : [];
        this.modelValidations.push(validation);
    },
    getModelErrors: function (value) {
        var errors = Ext.create('Ext.data.Errors'),
            validations = this.modelValidations,
            validators = Ext.data.validations,
            length, validation, field, valid, type, i;

        if (validations) {
            length = validations.length;

            for (i = 0; i < length; i++) {
                validation = validations[i];
                field = validation.field || validation.name;
                type = validation.type;
                valid = validators[type](validation, value);

                if (!valid) {
                    errors.add({
                        field: field,
                        message: validation.message || validators[type + 'Message']
                    });
                }
            }
        }
        return errors;
    },
    validateValue: function (value) {                                   //重写了这个方法实现校验，实现了Field上自定义的VType与Model校验同时校验
        var me = this,
            errors = me.getErrors(value),
            modelErrors = me.getModelErrors(value),
            isValid = Ext.isEmpty(errors) && modelErrors.isValid();
        if (!me.preventMark) {
            if (isValid) {
                me.clearInvalid();
            }
            else {
                if (!modelErrors.isValid()) {
                    modelErrors.each(function () {
                        errors.push(this.message);
                    })
                }
                me.markInvalid(errors);
            }
        }
        return isValid;
    }
})

Ext.override(Ext.form.Basic, {
    loadRecord: function (record) {
        this._record = record;
        this.setModelValidations(record.validations);
        return this.setValues(record.data);
    },
    setModelValidations: function (validations) {
        var fields = this.getFields(), i;
        for (i = 0; i < validations.length; i++) {
            var fieldMatch = this.findField(validations[i].field);
            if (fieldMatch) {
                fieldMatch.setModelFieldValidation(validations[i])
            }
        }
    }
})
//---------end override form<-->model validation-------------------

Ext.require(['Ext.container.Viewport', 'Ext.panel.Panel']);

Ext.application({
    enableQuickTips: true,
    name: 'app',
    appFolder: '/ExtClient/app',    
    autoCreateViewport: true,
    controllers: ['AppMainController'],

    launch: function () {
        Ext.QuickTips.init();

        var c = this.getController('AppMainController');
        c.application = this;        
    }
});